const app = getApp()

Page({
    /**
     * 页面的初始数据
     */
    data: {
        // label : {
        //     label_id : '' ,
        //     label_content : ''
        // } ,
        height : 0 ,
        xImage : [] ,
        value : '' ,
        xTags : [] ,
        xTagsBox : false ,
        namestate : false,
        title : '' ,
        userInfo:{},
        pageType:'other'
    },
    onLoad: function (options) {
        let that = this
        that.xUser()
        that.xWidths()
        if(options.pageType){
          that.setData({
            pageType:options.pageType
          })
        }
        const eventChannel = that.getOpenerEventChannel()
        eventChannel.on('xToAdd', function (data) {
          if(data.title){
            that.setData({
              title:data.title
            })
          }
          if(data.text_content){
            that.setData({
              value:data.text_content
            })
          }
          if(data.files){
            that.setData({
              xImage:data.files
            })
          }
        })
        // this.xGetTags()
    },
    onShow : function () {
      
    },
    xChildReload: function () {
      var pages = getCurrentPages();//当前页面栈
      if (pages.length > 1) {
        var beforePage = pages[pages.length - 2];//获取上一个页面实例对象
        beforePage.xParReload()
      }
    },
    xUser:function(){
      let that = this;
      wx.cloud.callFunction({
        name: 'user',
        data: {
            action: 'detail'
        }
      }).then(res => {
          let user = res.result
          that.setData({
            userInfo:res.result,
            openid: user.user_id
          })
      })
    },
    xInfo :function(){
      let that = this;
      wx.cloud.callFunction({
        name: 'user',
        data: {
            action: 'detail'
        }
      }).then(res => {
          let user = res.result
          that.setData({
            userInfo:res.result,
            openid: user.user_id
          })
      })
      app.globalData.InfoRefresh=true
      // var pages = getCurrentPages();//当前页面栈
      // if (pages.length > 1) {
      //   var beforePage = pages[pages.length - 2];//获取上一个页面实例对象
      //   beforePage.xInfo();//触发父页面中的方法
      // }
    },
    xWidths : function () {
        let that = this
        const query = wx.createSelectorQuery()
        query.select('#xWidths').boundingClientRect()
        query.exec( function (res) {
            let width = res[0].width
            that.setData({
                height : width
            })
        })
    },

    xUpload : function () {
        let that = this
        let id =  app.globalData.xCustomer.user_id
        let img = that.data.xImage
        wx.chooseImage({
            count: 1,
            sizeType: ['original', 'compressed'],
            sourceType: ['album', 'camera'],
            success: function (res) {
                wx.showLoading({
                    title: '上传中',
                })
                console.log(res.tempFilePaths[0])
                wx.cloud.uploadFile({
                    cloudPath: 'profile/' + id + "/" + new Date().getTime() + '.png',
                    filePath: res.tempFilePaths[0]
                }).then( xRes => {
                    wx.cloud.getTempFileURL({
                        fileList: [xRes.fileID],
                        success: xEnd => {
                            let path = xEnd.fileList[0].tempFileURL
                            img.push(path)
                            that.setData({
                                xImage : img
                            })
                            wx.setNavigationBarTitle({
                                title: '发帖' 
                            })
                            wx.hideLoading()
                        }
                    })
                })
            }
        })
    },

    xPreviewImage : function (e) {
        let that = this
        let path = e.currentTarget.dataset.path
        let ximg = that.data.xImage
        wx.previewImage({
            current: path, // 当前显示图片的http链接
            urls: ximg // 需要预览的图片http链接列表
        })
    },

    xDelete : function (e) {
        let that = this
        let index = e.currentTarget.dataset.index
        let ximg = that.data.xImage
        ximg.splice(index , 1)
        that.setData({
            xImage : ximg
        })
    },

    xValue : function (e) {
        let that = this
        let value = e.detail.value
        that.setData({
            value : value
        })
    },

    xSave : function (e) {
        wx.showLoading({
            title: '提交中',
        })
        let that = this
        let value = that.data.value
        let images = that.data.xImage
        let title = that.data.title
        if(that.data.userInfo.nickName === undefined) {
            app.authorizeLogin(e , async function(){});
        }
        let data = {}
        if(value === '' ||title==='' ){
            wx.showToast({
                title: '请输入帖子内容',
                icon: 'none'
            })
            return false
        }
        data = {
            title:that.data.title,//话题标题内容
            text_content:that.data.value,
            files : images ,
        }
        wx.cloud.callFunction({
            name: 'dynamic_content',
            data: {
                action: "add_dynamic_content",
                dataObj: data 
                // fictitious_openid : ''
            }
        }).then(res => {
            wx.showToast({
                title : '创建成功' ,
                icon : 'none' ,
                success : () => {
                    setTimeout( () => {
                        // 首页发布成功跳转至社区
                        if(that.data.pageType=='index'){
                          wx.redirectTo({
                            url:'/subtool/pages/community/index/index'
                          })
                          
                        }else{
                          // 重新编辑后发布跳转回上一页
                          wx.navigateBack({
                            delta:1
                          })  
                          getApp().globalData.scrollTop=true
                          that.xChildReload() //重新编辑后父页面设置scrollTop：0
                        }     
                    } , 1000)
                }
            })
        })
    },

    xGetTags : function () {
        let that = this
        wx.cloud.callFunction({
            name: 'dynamic_content',
            data: {
                action: "get_labels"
               }
        }).then(res => {
            console.log("返回:", res.result)
            let data = res.result
            that.setData({
                xTags : data
            })
        })
    },

    xSelectTags : function (e) {
        let that = this
        let key = e.currentTarget.dataset.key
        let xTags = that.data.xTags
        that.setData({
            label : {
                label_id : xTags[key]._id ,
                label_content : xTags[key].label_content
            },
            xTagsBox : false
        })
    },

    bindPickerChange: function (e) {
        console.log(e)
        let that = this
        let key = e.detail.value
        let xTags = that.data.xTags
        that.setData({
            label : {
                label_id : xTags[key]._id ,
                label_content : xTags[key].label_content
            }
        })
    },


    xShowTagsBox : function () {
        let that = this
        let xTagsBox = that.data.xTagsBox
        that.setData({
            xTagsBox : !xTagsBox
        })
    },

    xCloseName: function () {
      let that = this
      let namestate = that.data.namestate
      that.setData({
        namestate : !namestate
      })
      
      console.log(that.data.namestate)
    },

    xBack: function () {
      wx.navigateBack({
        delta: -1,
      })
    },
    // 去授权
    xToLogin:function(){
      wx.navigateTo({
        url: '/pages/navigation/login/index?path='+'/subtool/pages/community/add/index',
      })
    },
    xToEditUser:function(){
      wx.navigateTo({
        url: '/subuser/pages/personal_information/index',
      })
    },
    xTitle: function (e) {
      let that = this
      let value = e.detail.value
      that.setData({
        title : value
      })
    }
})